-- slime_weaken
-- create by zhuangsl
-- 延迟怪物主动技的释放时间(对全体怪物起效)

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 专属技能等级
        local level;
        local prop;
        extra = extra or {};
        if extra["level"] then
            level = tonumber(extra["level"]);
        else
            prop = PropM.combine(source, "gain_slime_skill", para[1]);
            level = PropM.apply(prop, 0);
        end

        -- 场上活着的怪物
        local targets = {};
        local grids = DungeonM.getCurrentDungeon();
        for _, grid in ipairs(grids) do
            if  grid:isMonster() and
                grid:isOpened() and
                not grid.monster:isDead() then
                table.insert(targets, grid.monster);
            end
        end

        -- 默认每个怪物只有一种主动技
        local remoteProps = {"remote_magic","remote_attack","suicide_attack","flee","remote_magic2"};
        local props, activeId, effectRound;
        local propId = PropM.getPropId("add_cd");
        for _, target in ipairs(targets) do
            repeat
                if SlimeSkillM.defyImmunitySkill(skillId) then
                    -- 免疫冈布奥主动技
                    prop = PropM.combine(target, "slime_skill_immunity", 1);
                    if prop[3] > 0 then
                        if not SkillM.getSequence(target) then
                            SkillM.initSequence(source, target, skillId);
                        end
                        SkillM.getSequence(target):immunity(source, target, skillId);
                        break;
                    end
                end

                for index = 1, #remoteProps do
                    props = PropM.fetchProps(target, remoteProps[index]);

                    -- 是否免疫了增加CD
                    prop = PropM.combine(target, "immunity_add_cd", 1);

                    if #props > 0 and prop[3] <= 0 then
                        activeId = props[1][1];
                        effectRound = para[2] * level + 1;

                        -- 如果是BOSS，需要计算抗性
                        if target:isBoss() then
                            -- 抗性
                            prop = PropM.combine(target, "resist", "add_cd");
                            effectRound = PropM.apply(prop, effectRound - 1) + 1;

                            -- 累积抗性，记在mixed下防止被清除掉
                            if prop[3] < 750 then
                                prop = { prop[1], prop[2], math.min(250, 750 - prop[3]), prop[4] };
                                PropM.record(target, prop, "mixed");
                            end
                        end

                        -- 如果已经被延后过了,回合数累积
                        props = PropM.fetchProps(target, propId);
                        if #props > 0 then
                            effectRound = effectRound + props[1][4] - round;
                            -- 先清掉原来的再重新附加上去
                            PropM.deletePropById(target, "special", propId);
                        end

                        prop = {propId, activeId, effectRound, effectRound + round};
                        PropM.record(target, prop, "special");

                        -- 增加命中动作
                        if SkillM.getSequence(target) == nil then
                            SkillM.getSequenceList()[target] = CastSequence.create(source, target, skillId);
                        end

                        SkillM.getSequence(target):hit(source, target, skillId);

                        break;
                    end
                end

                -- 幽灵船、冈布奥怪、活动本BOSS
                if target.type == OBJECT_TYPE_GHOST_SHIP or target:isSlime()
                    or ActivityBossM.isSpecialBoss(target) then
                    -- 如果已经被延后过了,回合数累积
                    effectRound = para[2] * level + 1;

                    -- 如果是BOSS，需要计算抗性
                    if target:isBoss() then
                        -- 抗性
                        prop = PropM.combine(target, "resist", "add_cd");
                        effectRound = PropM.apply(prop, effectRound - 1) + 1;

                        -- 累积抗性，记在mixed下防止被清除掉
                        if prop[3] < 750 then
                            prop = { prop[1], prop[2], math.min(250, 750 - prop[3]), prop[4] };
                            PropM.record(target, prop, "mixed");
                        end
                    end

                    props = PropM.fetchProps(target, propId);
                    if #props > 0 then
                        effectRound = effectRound + props[1][4] - round;
                        -- 先清掉原来的再重新附加上去
                        PropM.deletePropById(target, "special", propId);
                    end

                    prop = {propId, "slime", effectRound, effectRound + round};
                    PropM.record(target, prop, "special");

                    -- 增加命中动作
                    if SkillM.getSequence(target) == nil then
                        SkillM.getSequenceList()[target] = CastSequence.create(source, target, skillId);
                    end

                    SkillM.getSequence(target):hit(source, target, skillId);
                end
            until true
        end

        -- 返回作用目标
        return targets;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2]), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 专属技能等级
        local prop = PropM.combine(source, "gain_slime_skill", para[1]);
        local level = PropM.apply(prop, 0);

        local round = para[2] * level;

        desc = string.gsub(desc, "{round}", round);

        return desc;
    end,
};
